package com.innovation.chengzhangriji.ui;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.event.BaseEvent;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Method;
import java.security.Permission;

import de.greenrobot.event.Subscribe;

public class WelcomeActivity extends BaseActivity {

    private static final int STATE_INIT = -1;

    private static final int STATE_GUIDE = 0;

    private static final int STATE_LOGIN = 1;

    private static final int STATE_TIMELINE = 2;

    private int mState = STATE_INIT;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
    }

    @Override
    protected void permissionGranted() {
        new Thread(new Counter()).start();
        checkState();
    }

    @Override
    protected void onPageStart() {
        MobclickAgent.onPageStart(Config.UMENG_PAGE_WELCOME);
    }

    @Override
    protected void onPageEnd() {
        MobclickAgent.onPageEnd(Config.UMENG_PAGE_WELCOME);
    }

    // 检测状态
    private void checkState() {
        if (!Config.getGuide(this)) {
            mState = STATE_GUIDE;
        } else {
            LoginInfo login = Config.getLoginInfo(this);
            if (login != null) {
                mState = STATE_TIMELINE;
            } else {
                mState = STATE_LOGIN;
            }
        }
    }

    private void startGuideActivity() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                startActivity(GuideActivity.class);
            }
        });
    }

    private void startLoginActivity() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                startActivity(LoginActivity.class);
            }
        });
    }

    private void startMainActivity() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                startActivity(HomeActivity.class);
            }
        });
    }

    private void startActivity(Class<?> cls) {
        Intent intent = new Intent(WelcomeActivity.this, cls);
        startActivity(intent);
        finish();
        // 加载本地 Config
        Config.loadConfig(this);
        // 同步 Config 服务
        Config.startSyncConfigService(this);
        // 检测版本服务
        Config.startCheckVersionService(this);
    }

    private Handler mHandler = new Handler();

    private class Counter implements Runnable {

        private static final int MAX_COUNT = 1;

        private static final int SLEEP_TIME = 500;

        @Override
        public void run() {
            int count = 0;
            try {
                // 确保界面最少保持时间
                while (count < MAX_COUNT) {
                    count++;
                    Thread.sleep(SLEEP_TIME);
                }
                // 确保已经获取到应该有的状态
                while (mState == STATE_INIT) {
                    Thread.sleep(SLEEP_TIME);
                }
                switch (mState) {
                    case STATE_LOGIN:
                        startLoginActivity();
                        break;
                    case STATE_GUIDE:
                        startGuideActivity();
                        break;
                    case STATE_TIMELINE:
                        startMainActivity();
                        break;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
